package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.pojo.Course;

import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 课程管理控制器
 */
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 新增课程
     */
    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        courseService.addCourse(course);
        return Result.success();
    }

    /**
     * 更新课程信息
     */
    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        courseService.updateCourse(course);
        return Result.success();
    }

    /**
     * 根据ID查询课程信息
     */
    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Integer id) {
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }

    /**
     * 根据ID删除课程信息
     */
    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable Integer id) {
        courseService.deleteCourse(id);
        return Result.success();
    }

    /**
     * 条件分页查询课程列表
     */
    @GetMapping
    public Result getCourses(String name,Integer subject,Integer target,
                             @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<Course> coursePage = courseService.getCourses(name, subject, target, page, pageSize);
        return Result.success(coursePage);
    }
}